package com.wlyy.bcwlw.sys;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import com.wlyy.bcwlw.common.exception.AuthenticationFailedException;
import com.wlyy.bcwlw.sys.permission.entity.PermissionDTO;
import com.wlyy.bcwlw.sys.role.entity.RoleDTO;
import com.wlyy.bcwlw.sys.user.entity.UserProfile;

/**
 * @ClassName. SecurityFacade
 * @Description. 权限当前用户
 * @author zhangyang
 * @date 2015年8月31日 上午11:48:41
 * @version V1.0
 */
public abstract class SecurityFacade {
	
	private static String ADMIN_CURRENT_AREAID ="";

	/**
	 * 
	* @Title. getCurUserId
	* @Description. 当前用户Id
	* @return String
	* @exception.
	 */
	public static  String getCurUserId(){
		UserProfile up = SecurityContextHolder.getContext().getUserProfile();
		if( up == null || up.isAnonymous() ) {
			throw new AuthenticationFailedException("用户已失效，请重新登录");
		}
		return up.getUserId();
	}
	
	
	/**
	 * 
	* @Title. getCurUserIdNotThrowExceptionIfAnonymous
	* @Description. 获取当前用户id，即使是用户不存在或匿名也不报错，返回Null
	* @return String
	* @exception.
	 */
	public static String getCurUserIdNotThrowExceptionIfAnonymous() {
		UserProfile up = SecurityContextHolder.getContext().getUserProfile();
		if(up == null || up.isAnonymous()) {
			return null;
		}
		return up.getUserId();
	}
	
	/**
	 * 
	* @Title. getUserProfile
	* @Description. 当前用户UserProfile
	* @return UserProfile
	* @exception.
	 */
	public static UserProfile getUserProfile() {
		return SecurityContextHolder.getContext().getUserProfile();
	}
	
	/**
	 * 
	* @Title. getUserIp
	* @Description. 获取挡前用户IP地址
	* @return String
	* @exception.
	 */
	public static String getUserIp(){
		UserProfile up = SecurityContextHolder.getContext().getUserProfile();
		if(up == null || up.isAnonymous()) {
			return null;
		}
		return up.getIp();
	}
	
	/**
	 * 
	* @Title. getCurUserInfo
	* @Description. 当前人用户信息
	* @return String
	* @exception.
	 */
	public static String getCurUserInfo() {
		if(StringUtils.isEmpty(getUserProfile().getName())) {
			return getUserProfile().getUserName();
		}
 		return getUserProfile().getName()+"("+getUserProfile().getUserName()+")";
	}
	
	
	
	public static void setCurrentAreaId(String areaId){
		ADMIN_CURRENT_AREAID = areaId;
	}
	
	/**
	 * 
	* @Title. getCurrentAreaId
	* @Description. 当前人地区id
	* @return String
	* @exception.
	 */
	public static String getCurrentAreaId(){
		if(hasRole(SecurityConst.ROLE_ADMIN_MARK)){
			return ADMIN_CURRENT_AREAID;
		}
		return SecurityContextHolder.getContext().getUserProfile().getUserAreaId();
	}
	
	/**
	 * 
	* @Title. hasRole
	* @Description. 判断当前用户是否有某个角色
	* @param roleIdOrRoleMark   角色id 或 角色标识
	* @return boolean
	* @exception.
	 */
	public static boolean hasRole(String roleIdOrRoleMark) {
		List<RoleDTO> roleList = getUserProfile().getRoles();
		if (roleList == null || roleList.size() == 0) {
			return false;
		}
		for (RoleDTO role : roleList) {
			if(roleIdOrRoleMark.equals(role.getId()) || roleIdOrRoleMark.equals(role.getCode())) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 
	* @Title. hasPermission
	* @Description. 判断当前用户是否有某个权限
	* @param permissionsign  权限id 或 权限标识
	* @return boolean
	* @exception.
	 */
	public static boolean hasPermission(String permissionsign) {
		Assert.notNull(permissionsign);

		List<PermissionDTO> permissionList = getUserProfile().getPermissions();
		for (PermissionDTO permissionDTO : permissionList) {
			if (permissionsign.equals(permissionDTO.getId()) || permissionsign.equals(permissionDTO.getCode())) {
				return true;
			}
		}
		return false;
	}
	
	
	
	
	
}
